package com.hzz.framework.scheduling;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.support.CronTrigger;

@Slf4j
public abstract class TriggerTask implements ITriggerTask {

    @Getter
    @Setter
    private String cron;
    private CronTrigger trigger;

    @Override
    public String jobName() {
        return this.getClass().getSimpleName().toLowerCase();
    }


    @Override
    public CronTrigger getTrigger() {
        if (trigger == null) {
            trigger = new CronTrigger(cron);
        }
        return trigger;
    }

    @Override
    public CronTrigger setTrigger(String cron) {
        this.cron = cron;
        String old = trigger.getExpression();
        this.trigger = new CronTrigger(cron);
        log.info("update cron success, old: {} , new: {}", old, trigger.getExpression());
        return this.trigger;
    }

    @Override
    public String toString() {
        return "TriggerTask{" +
                "jobName=" + jobName() +
                ",task=" + getTask() +
                ",cronTrigger=" + getTrigger().getExpression() +
                '}';
    }
}